package com.itheima.bos.web.action;

import com.itheima.bos.domain.Region;
import com.itheima.bos.domain.Subarea;
import com.itheima.bos.service.ISubareaService;
import com.itheima.bos.utils.FileUtils;
import com.itheima.bos.web.action.base.BaseAction;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Mr.King on 2017/3/12.
 */
@Component
@Scope(value = "prototype")
public class SubareaAction extends BaseAction<Subarea> {

    @Autowired
    private ISubareaService subareaService;

    //保存添加或修改
    public String saveOrUpdate(){
        subareaService.saveOrUpdate(model);
        return "list";
    }

    //分页查询
    public String pageQuery() throws IOException {
        setPage();

        String addresskey = model.getAddresskey();
        Region region = model.getRegion();
        DetachedCriteria criteria = pageBean.getDetachedCriteria();

        if (StringUtils.isNotBlank(addresskey)){
            //按照地址关键字进行查询
            criteria.add(Restrictions.like("addresskey","%"+addresskey+"%"));
        }

        if (region != null){
            DetachedCriteria criteriaCriteria = criteria.createCriteria("region");

            String province = region.getProvince();//省
            String city = region.getCity();//市
            String district = region.getDistrict();//区

            if (StringUtils.isNotBlank(province)){
                //按照省进行模糊查询
                criteriaCriteria.add(Restrictions.like("province","%"+province+"%"));
            }
            if (StringUtils.isNotBlank(city)){
                //按照市进行模糊查询
                criteriaCriteria.add(Restrictions.like("city","%"+city+"%"));
            }
            if (StringUtils.isNotBlank(district)){
                //按照区进行模糊查询
                criteriaCriteria.add(Restrictions.like("district","%"+district+"%"));
            }
        }
        subareaService.pageQuery(pageBean);
        String[] arr = new String[]{ "subareas",
                "decidedzone", "detachedCriteria", "pageSize", "currentPage" };
        toJSON(pageBean,arr);
        return NONE;
    }

    //导出下载
    public String exportXls() throws IOException {
        // 查询所有的分区数据
        List<Subarea> list = subareaService.findAll();
        // 将list集合中的数据写到一个Excel文件中
        HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件，当前这个文件在内存中
        HSSFSheet sheet = workbook.createSheet("分区数据");// 创建一个sheet页
        HSSFRow headRow = sheet.createRow(0);// 创建标题行
        headRow.createCell(0).setCellValue("分区编号");
        headRow.createCell(1).setCellValue("区域编码");
        headRow.createCell(2).setCellValue("关键字");
        headRow.createCell(3).setCellValue("起始号");
        headRow.createCell(4).setCellValue("结束号");
        headRow.createCell(5).setCellValue("单双号");
        headRow.createCell(6).setCellValue("位置信息");
        for (Subarea subarea : list) {// 循环list，将数据写到Excel文件中
            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
            dataRow.createCell(0).setCellValue(subarea.getId());
            dataRow.createCell(1).setCellValue(subarea.getRegion().getId());
            dataRow.createCell(2).setCellValue(subarea.getAddresskey());
            dataRow.createCell(3).setCellValue(subarea.getStartnum());
            dataRow.createCell(4).setCellValue(subarea.getEndnum());
            dataRow.createCell(5).setCellValue(subarea.getSingle());
            dataRow.createCell(6).setCellValue(subarea.getPosition());
        }

        // 文件下载：一个流（输出流）、两个头
        ServletOutputStream out = ServletActionContext.getResponse()
                .getOutputStream();

        String filename = "分区数据.xls";
        filename = FileUtils.encodeDownloadFilename(filename,
                ServletActionContext.getRequest().getHeader("user-agent"));
        ServletActionContext.getResponse().setContentType(
                ServletActionContext.getServletContext().getMimeType(filename));

        ServletActionContext.getResponse().setHeader("content-disposition",
                "attachment;filename=" + filename);
        workbook.write(out);
        return NONE;
    }

    //定区异步请求查询
    public String findListByAjax() throws IOException {
        DetachedCriteria criteria = DetachedCriteria.forClass(Subarea.class);
        criteria.add(Restrictions.isNull("decidedzone"));

        List<Subarea> list = subareaService.findListByAjax(criteria);
        String[] arr = new String[]{"decidedzone","region","startnum","endnum","single"};
        toJSON(list,arr);
        return NONE;
    }
}
